package com.yisoo.rs.rsmovie.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.yisoo.rs.common.vo.MovieJsonVo;
import com.yisoo.rs.rsmovie.entity.AppEntity;
import com.yisoo.rs.rsmovie.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yisoo.rs.rsmovie.entity.MoviesEntity;
import com.yisoo.rs.rsmovie.service.MoviesService;
import com.yisoo.rs.common.utils.PageUtils;
import com.yisoo.rs.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-12-03 17:03:58
 */
@RestController
@RequestMapping("rsmovie/movies")
public class MoviesController {
    @Autowired
    private MoviesService moviesService;

    @Autowired
    private AppService appService;

    @PostMapping("/quick_import")
    public R quickImport(@RequestParam String appName, @RequestBody List<MovieJsonVo> dataList){
//        创建本次应用id
        AppEntity appEntity = new AppEntity(null, appName, null, null, null, null);
        boolean save = appService.save(appEntity);
        if (!save){
            return R.error("应用创建失败");
        }
        boolean isImport = moviesService.quickImport(appEntity.getAppId(),dataList);
        if(isImport){
            return R.ok();
        }else{
            return R.error();
        }

    }



    @RequestMapping("/test")
    public R test(){
        return  R.ok("调用成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = moviesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{mvId}")

    public R info(@PathVariable("mvId") Long mvId){
		MoviesEntity movies = moviesService.getById(mvId);

        return R.ok().put("movies", movies);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody MoviesEntity movies){
		moviesService.save(movies);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody MoviesEntity movies){
		moviesService.updateById(movies);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] mvIds){
		moviesService.removeByIds(Arrays.asList(mvIds));

        return R.ok();
    }

}
